package demo01;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

public class MY implements Filter{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    }

    @WebFilter(filterName = "MyFilter",urlPatterns = "/register.do")
    public class MyFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
        @Override
        public void doFilter(ServletRequest servletRequest,
                             ServletResponse servletResponse,
                             FilterChain filterChain)
                throws IOException, ServletException {
            servletResponse.setContentType("text/html;charset=UTF-8");
            String username = servletRequest.getParameter("username");
            String password = servletRequest.getParameter("password");
//        servletResponse.getWriter().write("第一级过滤-MyFilter");
//        servletResponse.getWriter().write(username);
//        filterChain.doFilter(servletRequest,servletResponse);
            if(password.length()>8 && password!=null){
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
//            servletResponse.getWriter().println("不放行,密码长度小于8");
//            servletRequest.getRequestDispatcher("/register.html").forward(servletRequest,servletResponse);
                servletResponse.getWriter().println("<html>");
                servletResponse.getWriter().println("<body>");
                servletResponse.getWriter().println("不放行,密码长度小于8<br>");
                servletResponse.getWriter().println("<button><a href='/register.html' style='text-decoration: none;'>返回注册页面</a></button>");
                servletResponse.getWriter().println("</body>");
                servletResponse.getWriter().println("</html>");
            }
        }

        @Override
        public void destroy() {

        }

    }
}
